/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Window Sample.

	Maxsi Window Sample is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Window Sample is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
	or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Window Sample. If not, see <http://www.gnu.org/licenses/>.

	MaxsiWindowSample.exe
	An example on how to use the Maxsi Window System for simple things.

	Main.cpp
	The example code on how to correctly create a simple Window and do simple
	things using the Maxsi Window System. This example file also demonstrates
	how to handle error conditions gracefully.

******************************************************************************/

#include "MaxsiEngine.h"
#include "Main.h"

MaxsiWindowSample::MaxsiWindowSample()
{
	Header			=	NULL;
	ProgressBar		=	NULL;
	StatusLabel		=	NULL;
	SubStatusLabel	=	NULL;
	cmdStats		=	NULL;
	cmdNews			=	NULL;
	cmdError		=	NULL;
	txtNews			=	NULL;
	txtError		=	NULL;
	SeparatorTop	=	NULL;
	SeparatorBottom	=	NULL;
}

MaxsiWindowSample::~MaxsiWindowSample()
{
	
}

bool MaxsiWindowSample::CreateControls()
{
	if (!ControlSystem()) { return false; }

	Header			=	ControlSystem()->CreateLabel(Window);
	ProgressBar		=	ControlSystem()->CreateProgressBar(Window);
	StatusLabel		=	ControlSystem()->CreateLabel(Window);
	SubStatusLabel	=	ControlSystem()->CreateLabel(Window);
	cmdStats		=	ControlSystem()->CreateButton(Window);
	cmdNews			=	ControlSystem()->CreateButton(Window);
	cmdError		=	ControlSystem()->CreateButton(Window);
	txtNews			=	ControlSystem()->CreateTextMultiLine(Window);
	txtError		=	ControlSystem()->CreateTextMultiLine(Window);
	SeparatorTop	=	ControlSystem()->CreateSeparatorH(Window);
	SeparatorBottom	=	ControlSystem()->CreateSeparatorH(Window);

	ControlSystem()->SetLabelText(Header, _MESTR("<b>Installing Altered Transmission - Demo 10</b>"));
	ControlSystem()->SetLabelText(StatusLabel, _MESTR("Downloading Altered Transmission/maps/Evasion_01.bsp"));
	ControlSystem()->SetLabelText(SubStatusLabel, _MESTR("Downloaded 164.5 MiB of 415.2 MiB (3.235 MiB/s)"));
	ControlSystem()->SetProgress(ProgressBar, 164.5f/415.2f, MAXSI_PROGRESS_INDETERMINATE);
	ControlSystem()->SetButtonText(cmdStats, _MESTR("Stats >"));
	ControlSystem()->SetButtonText(cmdNews, _MESTR("News >"));
	ControlSystem()->SetButtonText(cmdError, _MESTR("Errors >"));

	WindowSystem()->HideWindow(txtNews);
	WindowSystem()->HideWindow(txtError);

	return (Header && ProgressBar && StatusLabel && SubStatusLabel && cmdStats && cmdNews && cmdError && txtNews && txtError && SeparatorTop && SeparatorBottom);
}

bool MaxsiWindowSample::DeleteControls()
{
	return true;
}

bool MaxsiWindowSample::UpdateControls()
{
	MoveWindow(Header, 25, 30, Width-25-25, 20, true);
	MoveWindow(StatusLabel, 25, 10, Width-25-25, 90-60, true);
	MoveWindow(ProgressBar, 25, 90, Width-25-25, 18, true);
	MoveWindow(StatusLabel, 25, 60, Width-25-25, 90-60, true);
	MoveWindow(SubStatusLabel, 25, 115, Width-25-25, Height-115-3*24, true);
	MoveWindow(txtNews, 25, 130, Width-25-25, Height-110-3*24, true);
	MoveWindow(txtError, 25, 130, Width-25-25, Height-110-3*24, true);
	MoveWindow(cmdStats, Width-3*125, Height-2*24, 100, 24, true);
	MoveWindow(cmdNews, Width-2*125, Height-2*24, 100, 24, true);
	MoveWindow(cmdError, Width-1*125, Height-2*24, 100, 24, true);
	MoveWindow(SeparatorTop, 25, 50, Width-2*25, 3, true);
	MoveWindow(SeparatorBottom, 25, Height-2.5*24, Width-2*25, 3, true);

	return true;
}

MESTR* MaxsiWindowSample::GetWindowTitle()
{
	return BuildString(1, _MESTR("Maxsi Distribution"));
}

int MaxsiWindowSample::GetDefaultWidth()
{
	return 500;
}

int MaxsiWindowSample::GetDefaultHeight()
{
	return 370;
}

bool MaxsiWindowSample::UpdateWindowIcon()
{
	MaxsiHandle		Icon	=	WindowSystem()->CreatePictureBufferFromFile(_MESTR("../Images/MaxsiSVG.svg"));

	if (!Icon) { return false; }

	return WindowSystem()->SetWindowIcon(Window, Icon);
}
int main(int argc, char** argv)
{
	printf("%s", "Looks like main() was called!\n");

	MaxsiWindowSystem*	WindowSystem	=	CreateLocalWindowSystem();

	if ( WindowSystem )
	{
		if ( WindowSystem->Initialize(&argc, &argv) )
		{
			MaxsiWindow*	Window		=	new MaxsiWindowSample;

			if ( Window )
			{
				Window->SetWindowSystem(WindowSystem);

				if ( Window->CreateWindow() )
				{			
					WindowSystem->MainLoop();
				}
				else
				{					
					CriticalMsgBox(NULL, 0, _MESTR("An error occured while creating the main window."), _MESTR("Maxsi Window Sample"));			
				}
			}
			else
			{
				CriticalMsgBox(NULL, 0, _MESTR("Could not allocate a window using the local Maxsi Window System."), _MESTR("Maxsi Window Sample"));				
			}

			delete Window;
		}
		else
		{
			CriticalMsgBox(NULL, 0, _MESTR("Could not initialize the local Maxsi Window System."), _MESTR("Maxsi Window Sample"));
		}

		delete WindowSystem;
	}
	else
	{
		CriticalMsgBox(NULL, 0, _MESTR("Could not allocate the local Maxsi Window System."), _MESTR("Maxsi Window Sample"));
	}

	return 0;
}



#if 0
bool CreateWindow()
{
	Window			=	gtk_window_new(GTK_WINDOW_TOPLEVEL);

	gtk_window_set_title(GTK_WINDOW(Window), "Maxsi Distribution");
	gtk_window_set_default_size(GTK_WINDOW(Window), Width, Height);
	gtk_window_set_position(GTK_WINDOW(Window), GTK_WIN_POS_CENTER);
	gtk_window_set_icon(GTK_WINDOW(Window), CreatePictureBuffer("MaxsiSVG.svg"));
	//gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);
	gtk_widget_add_events(GTK_WIDGET(Window), GDK_CONFIGURE);

	Frame			=	gtk_fixed_new();

 	gtk_container_add(GTK_CONTAINER(Window), Frame);

	Header			=	gtk_label_new(NULL);
	ProgressBar		=	gtk_progress_bar_new();
	StatusLabel		=	gtk_label_new("Downloading Altered Transmission/maps/Evasion_01.bsp");
	SubStatusLabel	=	gtk_label_new("Downloaded 164.5 MiB of 415.2 MiB (3.235 MiB/s)");
	cmdStats		=	gtk_button_new_with_label("Stats >");
	cmdNews			=	gtk_button_new_with_label("News >");
	cmdError		=	gtk_button_new_with_label("Errors >");
	txtNews			=	NULL;
	txtError		=	NULL;
	SeperatorTop	=	gtk_hseparator_new();
	SeperatorBottom	=	gtk_hseparator_new();

	gtk_label_set_markup(GTK_LABEL(Header), "<b>Installing Altered Transmission - Demo 10</b>");

	ResizeWindow();

	gtk_widget_show_all(Window);

	g_signal_connect_swapped(G_OBJECT(Window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
	g_signal_connect(G_OBJECT(Window), "configure-event", G_CALLBACK(WindowResized), NULL);

	return true;
}

bool ResizeWindow()
{
	MoveWindow(Header,25,30,Width-25-25,20,TRUE);
	MoveWindow(StatusLabel,25,10,Width-25-25,90-60,TRUE);
	MoveWindow(ProgressBar,25,90,Width-25-25,18,TRUE);
	MoveWindow(StatusLabel,25,60,Width-25-25,90-60,TRUE);
	MoveWindow(SubStatusLabel,25,115,Width-25-25,Height-115-3*24,TRUE);
	MoveWindow(txtNews,25,130,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(txtError,25,130,Width-25-25,Height-110-3*24,TRUE);
	MoveWindow(cmdStats	,Width-3*125,Height-2*24,100,24,TRUE);
	MoveWindow(cmdNews	,Width-2*125,Height-2*24,100,24,TRUE);
	MoveWindow(cmdError	,Width-1*125,Height-2*24,100,24,TRUE);
	MoveWindow(SeperatorTop, 25, 50, Width-2*25, 3, TRUE);
	MoveWindow(SeperatorBottom, 25, Height-2.5*24, Width-2*25, 3, TRUE);	

	return true;
}

void WindowResized(GtkWindow *window, GdkEvent *event, gpointer data)
{
	if ( Width != event->configure.width || Height != event->configure.height )
	{
		Width	=	event->configure.width;
		Height	=	event->configure.height;

		//ResizeWindow();
	}
}

int main(int argc, char** argv)
{
	// Initialize GTK
	gtk_init(&argc, &argv);

	CreateWindow();

	//gtk_progress_set_activity_mode(GTK_PROGRESS(ProgressBar), TRUE);
	//gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(ProgressBar), GTK_PROGRESS_DISCRETE);

	gtk_main();

	return 0;
}
#endif

